package upgrade

import (
	"net/http"
	"net/url"
	"time"
	"appengine"
	"appengine/datastore"
)

func UpgradeOne2TwoHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	appv, err := getAppValue(ctx, "version")
	if err != nil {
		if err != datastore.ErrNoSuchEntity {
			ctx.Errorf("Upgrade Error: %v", err)
			w.Write([]byte("Upgrade Error!"))
			return
		} else {
			appv = "1"
		}
	}

	if appv != "1" {
		w.Write([]byte("Don't need Upgrade!"))
	}

	query := datastore.NewQuery("Version")
	var vvs []*Version
	_, err = query.GetAll(ctx, &vvs)
	if err != nil {
		w.Write([]byte(err.Error()))
	}

	var idx int64 = 0
	for _, v := range vvs {
		idx += 1

		path := v.Path
		var pk = datastore.NewKey(ctx, "Page", path, 0, nil)
		var key = datastore.NewKey(ctx, "Page", "", v.Latest, pk)
		var page = new(Page)
		err := datastore.Get(ctx, key, page )
		if err != nil {
			w.Write([]byte(err.Error()))
		}

		path, _ = url.QueryUnescape(path)
		var pa = &Path{Id:idx, Path: path, NewPath: "", Latest:1}
		var post = &Post{Id:idx, Title: page.Title, Content: page.Content, Author: page.Author, Date: page.Date, Version: 1, Path: path}

		var pok = datastore.NewKey(ctx, "Post", "", idx, nil)
		var pokey = datastore.NewKey(ctx, "Post", "", 1, pok)
		var pakey = datastore.NewKey(ctx, "Path", path, 0, nil)
		var snkey = datastore.NewKey(ctx, "Snapshot", "", idx, nil)

		_, err = datastore.Put(ctx, pokey, post)
		if err != nil {
			w.Write([]byte(err.Error()))
		}
		_, err = datastore.Put(ctx, snkey, post)
		if err != nil {
			w.Write([]byte(err.Error()))
		}
		_, err = datastore.Put(ctx, pakey, pa)
		if err != nil {
			w.Write([]byte(err.Error()))
		}
	}
	w.Write([]byte("version 1 to version 2 Upgraded"))
	setAppValue(ctx, "version", "2")
}

func UpgradeTwo2ThreeHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	appv, err := getAppValue(ctx, "version")
	if err != nil {
		if err != datastore.ErrNoSuchEntity {
			ctx.Errorf("Upgrade Error: %v", err)
			w.Write([]byte("Upgrade Error!"))
			return
		} else {
			appv = "2"
		}
	}

	if appv != "2" {
		w.Write([]byte("Don't need Upgrade!"))
	}

	query := datastore.NewQuery("Path")
	var pts []*Path
	_, err = query.GetAll(ctx, &pts)
	if err != nil {
		w.Write([]byte(err.Error()))
	}

	for _, p := range pts {
		var key = datastore.NewKey(ctx, "Snapshot", "", p.Id, nil)

		var snap = new(Post)
		err := datastore.Get(ctx, key, snap)
		if err != nil {
			w.Write([]byte(err.Error()))
		}
		snap.Path = p.Path
		_, err = datastore.Put(ctx, key, snap)
		if err != nil {
			w.Write([]byte(err.Error()))
		}
	}
	w.Write([]byte("version 2 to version 3 Upgraded"))
	setAppValue(ctx, "version", "3")
}

type Path struct {
	Id		int64
	Path	string
	NewPath	string
	Latest	int64
}

type Post struct {
	Id		int64
	Title 	string
	Content	string
	Author 	string
	Date 	time.Time
	Version	int64
	Path 	string
}

type Page struct {
	Path	string
	Title 	string
	Content	string
	Author 	string
	Date 	time.Time
	Version	int64
	Status	int64
}

type Version struct {
	Path	string
	Latest	int64
}
